<template>
  <div>
    <el-dialog
      :title="info.type == 'add' ? '新增' : '修改'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="rolename">
          <el-input v-model="ruleForm.rolename"></el-input>
        </el-form-item>
        <el-form-item label="权限">
          <!-- 获取和设置选中节点，就是操作输入框，得到输入框的值 -->
          <el-tree
            :data="treeData"
            show-checkbox
            default-expand-all
            node-key="id"
            ref="tree"
            highlight-current
            :props="defaultProps"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="info.isShow = false">取 消</el-button>
        <el-button v-if="info.type == 'add'" type="primary" @click="submitForm"
          >新增</el-button
        >
        <el-button v-else type="primary" @click="submitForm">修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getMenuListTree, roleAdd } from "../../http";
export default {
  props: ["info"],
  data() {
    return {
      // 下拉菜单的属性
      defaultProps: {
        children: "children",
        label: "title",
      },
      // 下拉菜单的数据
      treeData: [],
      ruleForm: {},
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // 获取树形控件的数据
    this.getTreeData();
  },
  methods: {
    async getTreeData() {
      let res = await getMenuListTree();
      this.treeData = res.list;
    },
    setValue(val) {
      // 根据父组件传来的值（新增？修改？）给表单填入数据
      this.ruleForm = { ...val };
      this.ruleForm.menus = this.ruleForm.menus.split(",");
      console.log(this.$refs.tree);
      this.$nextTick(() => {
        console.log(this.$refs.tree);
        // vue中的dom更新是异步的 nextTick会在dom更新结束后执行
        // 报错原因：弹窗还没有加载 就调用弹窗中的tree组件中的方法 所以报错
        // nextTick 会在弹窗加载后执行 此时 el-tree已经正常加载
        // setCheckedKeys 是根据后台的数组 在页面上勾选对象的权限
        this.$refs.tree.setCheckedKeys(this.ruleForm.menus);
      });
    },
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // getCheckedKeys 是el-tree自带的方法
          // this.ruleForm.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
          this.ruleForm.menus = this.$refs.tree.getCheckedKeys();
          // 通过校验 准备提交数据
          let res;
          if (this.info.type === "add") {
            res = await roleAdd(this.ruleForm);
          } else if (this.info.type === "edit") {
            res = await menuEdit(this.ruleForm);
          }
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: res.msg,
            });
            this.info.isShow = false;
            // 子传父 刷新list组件
            this.$emit("update");
          } else {
            console.log("error submit!!");
            return false;
          }
        }
      });
    },
  },
};
</script>

<style>
</style>